﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Public;

namespace QLNhanKhau
{
    public partial class UCUpdateChuyenDen : UserControl
    {
        public UCUpdateChuyenDen()
        {
            InitializeComponent();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtSoCK.Text == "")
            {
                MessageBox.Show("Nhập số sổ hộ khẩu cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSoCK.Focus();
                return;

            }
            int SoCK = int.Parse(txtSoCK.Text);
            List<ChuyenKhauPublic> list_ck = ChuyenKhauBLL.LoadChuyenDenBySoCK(SoCK);
            if (list_ck.Count ==0)
            {
                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSoCK.ResetText();
                txtSoCK.Focus();
                if (dgvChuyenKhau.DataSource != null)
                {
                    dgvChuyenKhau.DataSource = null;
                    txtHoTen.ResetText();
                    txtCMND.ResetText();
                    dateNgaySinh.ResetValue();
                    cmbNoiSinh.Refresh();
                    cmbQuanHe.Refresh();
                    cmbQuocTich.Refresh();
                    cmbTonGiao.Refresh();

                    cmbTinhCap.Refresh();
                    cmbQuanHuyenCap.Refresh();
                    cmbPhuongXaCap.Refresh();

                    cmbDanToc.Refresh();
                    cmbNguoiNhan.Refresh();
                    txtThuongTru.ResetText();
                    txtSoChuyenKhau.ResetText();
                    txtSoSHK.ResetText();
                    txtLyDoChuyen.ResetText();
                    dateNgayCap.ResetValue();
                    dateNgayNhan.ResetValue();
                    txtChuHo.ResetText();
                    txtNguoiCap.ResetText();
                }
                return;
            }
            else
            {

                dgvChuyenKhau.DataSource = list_ck;
                //dgvChuyenKhau.Columns[0].HeaderText = "Mã chuyển khẩu";
                //dgvChuyenKhau.Columns[1].HeaderText = "Mã nhân khẩu";
                dgvChuyenKhau.Columns[2].HeaderText = "Số";
                
                dgvChuyenKhau.Columns[3].HeaderText = "Nơi chuyển";
                dgvChuyenKhau.Columns[4].HeaderText = "Lý do chuyển";
                dgvChuyenKhau.Columns[5].HeaderText = "Ngày cấp";
                //dgvChuyenKhau.Columns[6].HeaderText = "Cán bộ nơi cấp";
                dgvChuyenKhau.Columns[7].HeaderText = "Người cấp";
                //dgvChuyenKhau.Columns[8].HeaderText = "Chức vụ người cấp";
                dgvChuyenKhau.Columns[9].HeaderText = "Nơi cấp";
                //dgvChuyenKhau.Columns[10].HeaderText = "Cán bộ nơi nhận";
                //dgvChuyenKhau.Columns[11].HeaderText = "Ngày nhận";
                dgvChuyenKhau.Columns[12].HeaderText = "Người nhận";
                //dgvChuyenKhau.Columns[13].HeaderText = "Chức vụ người nhận";
                //dgvChuyenKhau.Columns[14].HeaderText = "Thường trú";
                dgvChuyenKhau.Columns[15].HeaderText = "Chủ hộ";
                dgvChuyenKhau.Columns[16].HeaderText = "Quan hệ";
                dgvChuyenKhau.Columns[0].Visible = false;
                dgvChuyenKhau.Columns[1].Visible = false;
                //dgvChuyenKhau.Columns[2].Visible = false;
                //dgvChuyenKhau.Columns[3].Visible = false;
                //dgvChuyenKhau.Columns[4].Visible = false;
                //dgvChuyenKhau.Columns[5].Visible = false;
                dgvChuyenKhau.Columns[6].Visible = false;
                //dgvChuyenKhau.Columns[7].Visible = false;
                dgvChuyenKhau.Columns[8].Visible = false;
                //dgvChuyenKhau.Columns[9].Visible = false;
                //dgvChuyenKhau.Columns[10].Visible = false;
                dgvChuyenKhau.Columns[11].Visible = false;
                dgvChuyenKhau.Columns[12].Visible = false;
                dgvChuyenKhau.Columns[13].Visible = false;
                dgvChuyenKhau.Columns[14].Visible = false;
                dgvChuyenKhau.Columns[15].Visible = false;
                dgvChuyenKhau.Columns[16].Visible = false;
                //dgvChuyenKhau.Columns[16].Visible = false;
            }
        }
        int _MaCK;
        int _MaNK;
        private void dgvChuyenKhau_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewCell cell = null;

            foreach (DataGridViewCell select in dgvChuyenKhau.SelectedCells)
            {
                cell = select;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                _MaCK = (int)row.Cells[0].Value;
                _MaNK = (int)row.Cells[1].Value;
                groupBox1.Enabled = true;
                NhanKhauPublic nk = NhanKhauBLL.LoadNhanKhauByMaNK((int)row.Cells[1].Value);
                txtHoTen.Text = nk.HoTen;
                if ((bool)nk.GioiTinh == true)
                    sbtGioiTinh.Value = true;
                else
                    sbtGioiTinh.Value = false;
                dateNgaySinh.Value = (DateTime)nk.NgaySinh;
                cmbNoiSinh.SelectedValue = (int)nk.NoiSinh;
                cmbDanToc.SelectedValue = (byte)nk.MaDT;
                cmbQuocTich.SelectedValue = (byte)nk.MaQT;
                cmbTonGiao.SelectedValue = (byte)nk.MaTG;
                txtTenGoiKhac.Text = nk.TenGoiKhac;
                txtCMND.Text = nk.CMND;
                txtQueQuan.Text = nk.QueQuan;
                txtThuongTru.Text = row.Cells[14].Value.ToString();
                txtSoChuyenKhau.Text = row.Cells[2].Value.ToString();
                txtChuHo.Text = row.Cells[15].Value.ToString();
                txtLyDoChuyen.Text = row.Cells[4].Value.ToString();
                cmbQuanHe.SelectedValue = row.Cells[16].Value.ToString();

                txtNguoiCap.Text = row.Cells[7].Value.ToString();
                cmbChucVu.SelectedValue = (byte)row.Cells[8].Value;
                cmbNguoiNhan.Text = row.Cells[12].Value.ToString();
                cmbPhuongXaCap.SelectedValue = (int)row.Cells[9].Value;
                cmbTinhCap.SelectedValue = PhuongXaBLL.LoadPhuongXaByMaPX((int)row.Cells[9].Value).MaT;
                cmbQuanHuyenCap.SelectedValue = PhuongXaBLL.LoadPhuongXaByMaPX((int)row.Cells[9].Value).MaQH;
                cmbQuanHe.SelectedValue = (byte)row.Cells[16].Value;
                txtSoSHK.Text = NhanKhauBLL.LoadNhanKhauByMaNK(_MaNK).MaSHK.ToString();
                cmbChucVuNguoiNhan.SelectedValue = (byte)row.Cells[13].Value;
                dateNgayCap.Value = (DateTime)row.Cells[5].Value;
                dateNgayNhan.Value = (DateTime)row.Cells[11].Value;
                cmbPhuongXaCap.Text = PhuongXaBLL.LoadPhuongXaByMaPX((int)row.Cells[9].Value).TenPhuongXa;
                cmbQuanHuyenCap.Text = PhuongXaBLL.LoadPhuongXaByMaPX((int)row.Cells[9].Value).TenQuanHuyen;
                cmbTinhCap.Text = PhuongXaBLL.LoadPhuongXaByMaPX((int)row.Cells[9].Value).TenTinh;

                //txtChucVuNguoiNhan.Text = ChucVuBLL.LoadChucVuByMaCV((byte)row.Cells[13].Value).TenChucVu;
            }
            else
                groupBox1.Enabled = false;
        }

        private void UCUpdateCK_Load(object sender, EventArgs e)
        {
            cmbTonGiao.DataSource = TonGiaoBLL.LoadTonGiao();
            cmbTonGiao.DisplayMember = "TenTonGiao";
            cmbTonGiao.ValueMember = "MaTG";

            cmbDanToc.DataSource = DanTocBLL.LoadDanToc();
            cmbDanToc.DisplayMember = "TenDanToc";
            cmbDanToc.ValueMember = "MaDT";
            //load quoc tich
            cmbQuocTich.DataSource = QuocTichBLL.LoadQuocTich();
            cmbQuocTich.DisplayMember = "TenQuocTich";
            cmbQuocTich.ValueMember = "MaQT";
            //load quan hệ chủ hộ
            cmbQuanHe.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHe.DisplayMember = "TenQuanHe";
            cmbQuanHe.ValueMember = "MaQH";
            //load quan hệ khai sinh

            cmbNguoiNhan.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiNhan.DisplayMember = "HoTen";
            cmbNguoiNhan.ValueMember = "MaND";

            //cmbCanBoDangKi.Text = frmDangNhap.user_login.HoTen.ToString();
            cmbCanBoDangKi.Items.Add(frmDangNhap.user_login.HoTen.ToString());
            cmbCanBoDangKi.SelectedIndex = 0;
            //.SelectedIndex = 1;

            cmbNoiSinh.DataSource = TinhBLL.LoadTinh();
            cmbNoiSinh.DisplayMember = "TenTinh";
            cmbNoiSinh.ValueMember = "MaT";

            cmbTinhCap.DataSource = TinhBLL.LoadTinh();
            cmbTinhCap.DisplayMember = "TenTinh";
            cmbTinhCap.ValueMember = "MaT";
            cmbTinhCap.SelectedIndex = -1;

            //cmbQuanHuyenCap.DataSource = QuanHuyenBLL.LoadQuanHuyen();
            //cmbQuanHuyenCap.DisplayMember = "TenQuanHuyen";
            //cmbQuanHuyenCap.ValueMember = "MaQH";

            //cmbPhuongXaCap.DataSource = PhuongXaBLL.LoadPhuongXa();
            //cmbPhuongXaCap.DisplayMember = "TenPhuongXa";
            //cmbPhuongXaCap.ValueMember = "MaPX";

            cmbChucVu.DataSource = ChucVuBLL.LoadChucVu();
            cmbChucVu.DisplayMember = "TenChucVu";
            cmbChucVu.ValueMember = "MaCV";

            cmbChucVuNguoiNhan.DataSource = ChucVuBLL.LoadChucVu();
            cmbChucVuNguoiNhan.DisplayMember = "TenChucVu";
            cmbChucVuNguoiNhan.ValueMember = "MaCV";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtHoTen.Text == "" || txtQueQuan.Text == "" || txtSoChuyenKhau .Text == "" || txtChuHo.Text == "" || txtLyDoChuyen.Text == "" || txtNguoiCap.Text == "" ||
                dateNgayNhan.Text == "" || dateNgayNhan.Text == ""||cmbPhuongXaCap.SelectedValue==null)
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int SoChuyenKhau=int.Parse(txtSoChuyenKhau.Text);
                    //public static void UpdateChuyenDen(int SoChuyenKhau, byte MaQH, string ThuongTru, string LyDoChuyen, DateTime NgayCap, string NguoiCap, byte ChucVu, int NoiCap, string NguoiNhan, int ChucVuNguoiNhan, int MaCK)
            ChuyenKhauBLL.UpdateChuyenDen(SoChuyenKhau, (byte)cmbQuanHe.SelectedValue,txtChuHo.Text, txtThuongTru.Text, txtLyDoChuyen.Text, (DateTime)dateNgayNhan.Value, txtNguoiCap.Text, (byte)cmbChucVu.SelectedValue, (int)cmbPhuongXaCap.SelectedValue, cmbNguoiNhan.Text, (byte)cmbChucVuNguoiNhan.SelectedValue, _MaCK);
            MessageBox.Show("Cập nhật thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            btnTim_Click(sender, e);
            //UCUpdateChuyenDen_Load(sender, e);

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult resuft = MessageBox.Show("Xóa Chuyển đến này!", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            switch (resuft)
            {
                case DialogResult.OK:
                    //NhanKhauBLL.UpdateKhaiTu(_MaKT,null);
                    //KhaiTuBLL.DeleteKhaiTu(_MaCK);
                    //UCUpdateKhaiTu_Load(sender, e);
                    ChuyenKhauBLL.DeleteChuyenKhau(_MaCK);
                    NhanKhauBLL.DeleteNhanKhau(_MaNK);
                    MessageBox.Show("Đã xóa chuyển khẩu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case DialogResult.Cancel:
                    break;
            }
        }

       
        private void txtSoSHK_Leave(object sender, EventArgs e)
        {
            SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(int.Parse(txtSoSHK.Text));
            if (shk == null)
            {
                this.balloonTip2.ShowBalloon(txtSoSHK);
                this.balloonTip2.SetBalloonCaption(txtSoSHK, "Thông báo");
                this.balloonTip2.SetBalloonText(txtSoSHK, "Không có sổ sổ hộ khẩu này trong hệ thống");
                txtSoSHK.Focus();
            }
            else
            {
                List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);
                txtChuHo.Text = list_nk.Find(item => item.MaQH == 1).HoTen;
            }
        }

        private void cmbNguoiNhan_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbChucVuNguoiNhan.SelectedValue =NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue).MaCV;
            //txtChucVuNguoiNhan.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue).MaCV).TenChucVu;
        }

        private void cmbTinhCap_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbQuanHuyenCap.DataSource = QuanHuyenBLL.LoadQuanHuyenByMaT((int)cmbTinhCap.SelectedValue);
            cmbQuanHuyenCap.DisplayMember = "TenQuanHuyen";
            cmbQuanHuyenCap.ValueMember = "MaQH";
            cmbQuanHuyenCap.SelectedIndex = -1;
        }

        private void cmbQuanHuyenCap_SelectionChangeCommitted(object sender, EventArgs e)
        {
            cmbPhuongXaCap.DataSource = PhuongXaBLL.LoadPhuongXaByMaQH((int)cmbQuanHuyenCap.SelectedValue);
            cmbPhuongXaCap.DisplayMember = "TenPhuongXa";
            cmbPhuongXaCap.ValueMember = "MaPX";
            cmbPhuongXaCap.SelectedIndex = -1;
        }

        private void txtSoCK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoCK.Text.Length > 7 && !Char.IsControl(e.KeyChar)||e.KeyChar==13)
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
                btnTim_Click(sender, e);
        }

        
    }
}